<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author lzw
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="i-ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'ftds:ftdsAzj:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.azjShmc }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsFtdsFtdsAzjList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { ftdsAzjDelete, ftdsAzjListData } from '/@/api/ftds/ftdsAzj';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('ftds.ftdsAzj');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('爱租机数据管理管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('商户名称'),
        field: 'azjShmc',
        component: 'Input',
      },
      {
        label: t('订单号'),
        field: 'azjDdh',
        component: 'Input',
      },
      {
        label: t('债务人姓名'),
        field: 'azjZwrxm',
        component: 'Input',
      },
      {
        label: t('物流单号'),
        field: 'azjWldh',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('商户名称'),
      dataIndex: 'azjShmc',
      key: 'a.azj_shmc',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('订单号'),
      dataIndex: 'azjDdh',
      key: 'a.azj_ddh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('债务人姓名'),
      dataIndex: 'azjZwrxm',
      key: 'a.azj_zwrxm',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('债务人联系电话'),
      dataIndex: 'azjZwrlxdh',
      key: 'a.azj_zwrlxdh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('债务人身份证号'),
      dataIndex: 'azjZwrsfzh',
      key: 'a.azj_zwrsfzh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('债务人身份证地址'),
      dataIndex: 'azjZwrsfzdz',
      key: 'a.azj_zwrsfzdz',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('债务人民族'),
      dataIndex: 'azjZwrmz',
      key: 'a.azj_zwrmz',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('债务人通讯地址'),
      dataIndex: 'azjZwrtxdz',
      key: 'a.azj_zwrtxdz',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('订单签约价'),
      dataIndex: 'azjDdqyj',
      key: 'a.azj_ddqyj',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('订单买断价'),
      dataIndex: 'azjDdmdj',
      key: 'a.azj_ddmdj',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('分期期数'),
      dataIndex: 'azjFqqs',
      key: 'a.azj_fqqs',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('月租金'),
      dataIndex: 'azjYzj',
      key: 'a.azj_yzj',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('逾期期数'),
      dataIndex: 'azjYqqs',
      key: 'a.azj_yqqs',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('逾期本金'),
      dataIndex: 'azjYqbj',
      key: 'a.azj_yqbj',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('滞纳金'),
      dataIndex: 'azjZnj',
      key: 'a.azj_znj',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('违约金'),
      dataIndex: 'azjWyj',
      key: 'a.azj_wyj',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('租用日期'),
      dataIndex: 'azjZyrq',
      key: 'a.azj_zyrq',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('手机型号'),
      dataIndex: 'azjSjxh',
      key: 'a.azj_sjxh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('发货日期'),
      dataIndex: 'azjFhrq',
      key: 'a.azj_fhrq',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('物流单号'),
      dataIndex: 'azjWldh',
      key: 'a.azj_wldh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('签收日期'),
      dataIndex: 'azjQsrq',
      key: 'a.azj_qsrq',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('逾期日期'),
      dataIndex: 'azjYqrq',
      key: 'a.azj_yqrq',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('紧急联系人'),
      dataIndex: 'azjJjlxr',
      key: 'a.azj_jjlxr',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑爱租机数据管理'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'ftds:ftdsAzj:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: ftdsAzjListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/ftds/ftdsAzj/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await ftdsAzjDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
